﻿using SYH.DataAccess;
using SYH.Domain.Common;
using SYH.IBLL;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;

namespace SYH.Web.Models
{
    public class PublicCs
    {
        /// <summary>
        /// 获取文件的相对路径
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public static string GetFile(Guid aid,out string Suffix)
        {
            IEntityRepository<AccAssociation> _Attaervice = new EntityRepository<AccAssociation>(new EntityDbContext());
            var abean = _Attaervice.GetBo(aid);
            var file = abean.BusinessFile;
            Suffix = file.UploadFileSuffix;
            return file.DownLoaddPath;
        }

        /// <summary>
        /// 获取文件的相对路径
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public static string GetFile(Guid aid)
        {
            IEntityRepository<AccAssociation> _Attaervice = new EntityRepository<AccAssociation>(new EntityDbContext());
            var abean = _Attaervice.GetBo(aid);
            var file = abean.BusinessFile;
            return file.DownLoaddPath;
        }

        /// <summary>
        /// 获取文件的相对路径
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public static string GetFileBId(Guid fid)
        {
            IEntityRepository<BusinessFile> _Files = new EntityRepository<BusinessFile>(new EntityDbContext());
            var fbean = _Files.GetBo(fid);
            return fbean.DownLoaddPath;
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="fid"></param>
        public static void DelFile(Guid fid)
        {
            IEntityRepository<BusinessFile> _Files = new EntityRepository<BusinessFile>(new EntityDbContext());
            var file = _Files.GetBo(fid);
            var filepath = System.Web.HttpContext.Current.Server.MapPath(file.DownLoaddPath);
            if (File.Exists(filepath))
            {
                File.Delete(filepath);
            }
            _Files.DeleteBo(fid);
        }

        /// <summary>
        /// 删除附件
        /// </summary>
        /// <param name="aid"></param>
        public static void Delete(Guid aid)
        {
            IEntityRepository<AccAssociation> _Attaervice = new EntityRepository<AccAssociation>(new EntityDbContext());
            var abean = _Attaervice.GetBo(aid);
            if (abean != null)
            {
                var file = abean.BusinessFile;
                var filepath = System.Web.HttpContext.Current.Server.MapPath(file.DownLoaddPath);
                if (File.Exists(filepath))
                {
                    File.Delete(filepath);
                }
                _Attaervice.DeleteBo(aid);
            }
        }

        /// <summary>
        /// 将文件保存为附件
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public static Guid Save2Atta(Guid fid)
        {
            IEntityRepository<AccAssociation> _Attaervice = new EntityRepository<AccAssociation>(new EntityDbContext());
            IEntityRepository<BusinessFile> _Files = new EntityRepository<BusinessFile>(new EntityDbContext());
            var file = _Files.GetBo(fid);
            var atta = new AccAssociation();
            atta.AnnexID = file.ID;
            atta.InDate = DateTime.Now;
            atta.CorpID = Guid.NewGuid();
            _Attaervice.SaveBo(atta);
            return atta.ID;
        }
    }
}